#include "DetFinAutToRegGrammatica.h"

RegGrammatica<string> DetFinAutToRegGrammatica::detFinAutToRegGrammatica(DetFinAut<string> &detFinAut)
{
	//converteren volgens stelling 3.2
	list<char> symbols = detFinAut.getSymbolList();
	list<string> states = detFinAut.getStateList();
	list<Transition<string> > transitionList = detFinAut.getTransitionList();
	list<ProductionRule<string> > productionRules;
	list<Transition<string> >::iterator t;

	for(list<string>::iterator i = states.begin(); i!= states.end(); i++)
	{
		string fromState = *i;
		for(list<char>::iterator s = symbols.begin(); s != symbols.end(); s++)
		{
			char symbol = *s;
			list<Transition<string> > theStates;
			for(list<Transition<string> >::iterator list = transitionList.begin(); list != transitionList.end(); list++)
			{
				if(list->begin == fromState && list->symbol == symbol)
				{
					theStates.push_back(*list);
				}
			}
			for(t = theStates.begin(); t != theStates.end(); t++)
			{
				string toState = t->end;
				string fromStateRewrited = string("A") + fromState[1];
				string toStateRewrited = string("A") + toState[1];
				if(detFinAut.isEndState(toState))
				{
					ProductionRule<string> productionRuleEnd = ProductionRule<string>(fromStateRewrited, symbol);
					productionRules.push_back(productionRuleEnd);
				}
				ProductionRule<string> productionRule = ProductionRule<string>(fromStateRewrited, symbol, toStateRewrited );
				productionRules.push_back(productionRule);
			}
		}
	}
	string startRule = detFinAut.getStart();
	string startRuleRewrited = string("A") + startRule[1];

	RegGrammatica<string> result = *new RegGrammatica<string>(productionRules, startRuleRewrited);
	return result;
}